/*
 * File Name：ScanImageTitle.java
 * Copyright：Copyright 2008-2015 CiWong.Inc. All Rights Reserved.
 * Description： ScanImageTitle.java
 * Modify By：bin
 * Modify Date：2015年1月16日
 * Modify Type：Add
 */
package com.ciwong.mobilelib.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Scroller;

import com.ciwong.libs.utils.DeviceUtils;
import com.ciwong.mobilelib.R;

/**
 * 浏览图片标题栏
 *
 * @author bin
 * @version ciwong v.1.0 2015年1月16日
 * @since ciwong v.1.0
 */
public class ScanImageTitle extends LinearLayout {
    /**
     * 菜单速度
     */
    private static final int SHOW_SPEED = 200;

    private Scroller mScroller;

    private int screenHeight;

    private int height;

    private int bottomHeight;

    private boolean show = true;

    public boolean isShow() {
        return show;
    }

    public ScanImageTitle(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public ScanImageTitle(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ScanImageTitle(Context context) {
        super(context);
        init();
    }

    private void init() {
        mScroller = new Scroller(getContext());
        View.inflate(getContext(), R.layout.scan_image_title, this);
        measure(MeasureSpec.makeMeasureSpec(LayoutParams.WRAP_CONTENT,
                MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(
                LayoutParams.WRAP_CONTENT, MeasureSpec.EXACTLY));
        bottomHeight = getBottom();
        height = getMeasuredHeight() - 1;
        screenHeight = DeviceUtils.getScreenHeight();
    }

    /**
     * 显示
     */
    public void show() {
        setVisibility(VISIBLE);
        show = true;
    }

    /**
     * 隐藏
     */
    public void hide() {
        setVisibility(GONE);
        show = false;
    }

    public void initHide() {
        setMarginBottom(-height);
        show = false;
    }

    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()) {
            setMarginBottom(mScroller.getCurrY());
            invalidate();
        }
    }

    private void setMarginBottom(int marginBottom) {
        android.widget.RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) getLayoutParams();
        params.topMargin = marginBottom;
        setLayoutParams(params);
    }

}
